package org.budo.graph.service.impl;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

import org.budo.support.application.registry.BudoApplicationUtil;
import org.budo.support.freemarker.FreemarkerUtil;
import org.budo.support.lang.util.FileUtil;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.lang.util.ProcessUtil;
import org.budo.support.lang.util.SystemUtil;
import org.budo.support.spring.io.util.ResourceUtil;
import org.budo.time.Time;
import org.springframework.beans.factory.InitializingBean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class WriteDumpScriptServiceImpl implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            this.writeDumpScript();
        } catch (Throwable e) {
            log.error("#26 writeHealthzScript error, e=" + e, e);
        }
    }

    private void writeDumpScript() {
        log.info("#21 writeDumpScript, this=" + this);

        if (SystemUtil.isMac()) {
            log.warn("#41 isMac, return, " + SystemUtil.getOsName() + ", " + this);
            return;
        }

        String applicationName = BudoApplicationUtil.getApplicationName();
        if (null == applicationName || applicationName.trim().isEmpty()) {
            log.error("#31 budoGraphService is null, this=" + this);
            return;
        }

        String now = Time.now().toString("yyyy-MMdd-HHmmss-SSS");

        Map<String, Object> map = MapUtil.stringObjectMap("PID", ProcessUtil.getCurrentProcessId() + "", //
                "applicationName", applicationName, //
                "now", now);

        InputStream template = ResourceUtil.classPathResourceInputStream("dump-template.sh");

        File toDir = this.toDir();
        File to = new File(toDir, "dump-" + applicationName + ".sh");
        FreemarkerUtil.freemarkerRender(template, map, to);

        log.warn("#47 after write file, applicationName=" + applicationName + ", canonicalPath=" + FileUtil.getCanonicalPath(toDir));
    }

    private File toDir() {
        String catalinaHome = SystemUtil.getEnvOrProperty("catalina.home"); // /usr/local/data/install/tj-job-web // /tmp/tomcat.3482782920729435923.16080
        log.info("#71 toDir, catalinaHome=" + catalinaHome);

        if (null != catalinaHome && !catalinaHome.startsWith("/tmp/")) { // tomcat 项目
            return new File(catalinaHome);
        }

        // launcher 项目一般走这个
        String userDir = SystemUtil.getEnvOrProperty("user.dir"); // /usr/local/data/install/tj-user-web
        log.info("#51 toDir, user.dir=" + userDir);

        if ("/".equals(userDir) || "/root".equals(userDir)) {
            userDir = SystemUtil.getEnvOrProperty("sun.java.command"); // A 启动命令指定了完整的路径 B 先CD到目录 然后启动 jar

            log.info("#56 toDir, sun.java.command=" + userDir);
        }

        // command 可能是个文件 变成他所在的目录
        File to = new File(userDir);
        if (!to.isDirectory()) {
            log.info("#61 toDir, to=" + to + ", path=" + userDir); // spring boot 项目

            to = to.getParentFile();
        }

        log.info("#66 toDir, to=" + to + ", path=" + userDir);
        return to;
    }
}